import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI } from '@/api/user'

export default {
  state: {
    namespaced: true,
    token: getToken() || ''
  },
  mutations: {
    useLogin (state, token) {
      state.token = token
      setToken(token)
    },
    useRemoveToken (state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    async useLogin ({ commit }, userInfo) {
      const res = await loginAPI(userInfo)
      commit('useLogin', res.data.token)
    }
  }
}
